package com.example.hellodemo.datastorage;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.hellodemo.R;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;



public class FileActivity extends AppCompatActivity {
    private Button getBtn,saveBtn;
    private EditText editText;
    private TextView textView;
    private  final  String fileName = "text.txt";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared_preferences);

        /*
        *    查看 data 存放
        * CodeYYdeMacBook-Pro:~ codeyy$ adb shell
generic_x86:/ $ run-as com.example.hellodemo
generic_x86:/data/data/com.example.hellodemo $ ls
cache code_cache shared_prefs
generic_x86:/data/data/com.example.hellodemo $ cd shared_prefs
generic_x86:/data/data/com.example.hellodemo/shared_prefs $ ls
data.xml
        *
        * */
        getBtn = findViewById(R.id.sharedpreference_get);
        saveBtn = findViewById(R.id.sharedpreference_save);
        editText = findViewById(R.id.sharedpreference_et);
        textView = findViewById(R.id.sharedpreference_tv);

        saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save(editText.getText().toString());
            }
        });

        getBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText(read());
            }
        });

    }
    // 存储数据
    public void save(String content){
        FileOutputStream outputStream = null;
        try {
            outputStream = openFileOutput(fileName,MODE_PRIVATE);
            outputStream.write(content.getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    // 读取数据
    private String read(){
        FileInputStream inputStream = null;

        try {
            inputStream = openFileInput(fileName);
            byte[] buff = new byte[1024];
            StringBuffer sb = new StringBuffer("");
            int len = 0;
            while ((len = inputStream.read(buff)) > 0){
                sb.append(new String(buff,0,len));
            }
            return sb.toString();

        }catch (IOException e){

        }finally {
            if (inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }

}
